const fs = require("fs");
const path = require("path");
const marked = require("marked");
const browser = require("browser-sync");

const mdPath = path.join(__dirname, process.argv[2]);
const cssPath = path.resolve("gitHub.css");
console.log(cssPath);

fs.watch(mdPath, (eventType, filename) => {
  console.log(eventType, filename);
  fs.readFile(mdPath, "utf-8", function (err, result) {
    const html = marked.parse(result); // 而不是 marked(result)
    fs.readFile(cssPath, "utf-8", function (err, data) {
      const resethtml = temp
        .replace("{{content}}", html)
        .replace("{{style}}", `<style>${data}</style>`);
      fs.writeFile(path.join(__dirname, "zks.html"), resethtml, () => {
        console.log("succsess");
      });
    });
  });
});

const temp = `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  {{style}}
</head>
<body>
    {{content}}
</body>
</html>
`;

browser.init({
  browser: "",
  server: __dirname,
  watch: true,
  index: path.basename("zks.html"),
});
